using System;
using System.Collections.Generic;

namespace WeatherMcpClient.Views
{
    public class ConsoleWeatherView : IWeatherView
    {
        public void ShowTitle()
        {
            Console.WriteLine("🌤️ Weather HTTP Client - 天气查询工具");
            Console.WriteLine("=====================================");
            Console.WriteLine();
        }

        public void ShowCityCoordinates()
        {
            Console.WriteLine("🏙️ 常用城市坐标参考:");
            var cities = new[]
            {
                new { Name = "北京", Lat = 39.9042, Lon = 116.4074 },
                new { Name = "上海", Lat = 31.2304, Lon = 121.4737 },
                new { Name = "广州", Lat = 23.1291, Lon = 113.2644 },
                new { Name = "深圳", Lat = 22.3193, Lon = 114.1694 },
                new { Name = "杭州", Lat = 30.2741, Lon = 120.1551 },
                new { Name = "成都", Lat = 30.5728, Lon = 104.0668 },
                new { Name = "纽约", Lat = 40.7128, Lon = -74.0060 },
                new { Name = "伦敦", Lat = 51.5074, Lon = -0.1278 },
                new { Name = "东京", Lat = 35.6762, Lon = 139.6503 }
            };

            foreach (var city in cities)
            {
                Console.WriteLine($"   {city.Name}: {city.Lat}, {city.Lon}");
            }
            Console.WriteLine();
        }

        public string? ReadMainMenuChoice()
        {
            Console.WriteLine("请选择要执行的操作:");
            Console.WriteLine("1. 查询当前天气");
            Console.WriteLine("2. 查询3天天气预报");
            Console.WriteLine("3. 批量查询多个城市");
            Console.WriteLine("4. 退出");
            Console.Write("请输入选择 (1-4): ");
            return Console.ReadLine();
        }

        public (double? Latitude, double? Longitude) ReadCoordinates()
        {
            Console.Write("请输入纬度 (例如: 39.9042): ");
            if (!double.TryParse(Console.ReadLine(), out double latitude) || latitude < -90 || latitude > 90)
            {
                Console.WriteLine("无效的纬度，请输入-90到90之间的数字");
                return (null, null);
            }

            Console.Write("请输入经度 (例如: 116.4074): ");
            if (!double.TryParse(Console.ReadLine(), out double longitude) || longitude < -180 || longitude > 180)
            {
                Console.WriteLine("无效的经度，请输入-180到180之间的数字");
                return (null, null);
            }

            return (latitude, longitude);
        }

        public List<(string Name, double Lat, double Lon)> ReadBatchCoordinates()
        {
            Console.WriteLine("请输入多个城市的坐标（输入空行结束）:");
            Console.WriteLine("格式: 城市名称,纬度,经度");
            Console.WriteLine("示例: 北京,39.9042,116.4074");
            Console.WriteLine();

            var coordinates = new List<(string Name, double Lat, double Lon)>();
            while (true)
            {
                Console.Write("输入城市坐标 (或直接回车结束): ");
                var input = Console.ReadLine();
                if (string.IsNullOrEmpty(input)) break;

                var parts = input.Split(',');
                if (parts.Length != 3 || !double.TryParse(parts[1], out var lat) || !double.TryParse(parts[2], out var lon))
                {
                    Console.WriteLine("格式错误，请使用: 城市名称,纬度,经度");
                    continue;
                }
                coordinates.Add((parts[0], lat, lon));
            }

            return coordinates;
        }

        public void ShowSection(string title, string content)
        {
            Console.WriteLine();
            Console.WriteLine(title);
            Console.WriteLine(new string('=', 37));
            Console.WriteLine(content);
            Console.WriteLine(new string('=', 37));
        }

        public void ShowInfo(string message)
        {
            Console.WriteLine(message);
        }
    }
}

